package io.shallwe._admin.account;



import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Ret;

import io.shallwe._admin.role.RoleAdminService;
import io.shallwe.common.controller.BaseController;
import io.shallwe.common.model.Account;
import io.shallwe.common.model.Role;
/**
 * 后台账号
 * @author 王小伟
 * @date 2019-06-11 07:17:33
 */

public class AccountAdminController extends BaseController {
	@Inject
	AdminAccountService accountService;
	
	@Inject
	RoleAdminService roleService;
	
	public void index() {
		checkUrlPara(0);
		render("account.html");
	}


	public void list() {
		Integer offset = getParaToInt("offset",1);
        Integer pageSize = getParaToInt("pageSize",100);
        String userName=getPara("userName");
        String realName=getPara("realName");
        renderJson(accountService.paginate(offset, pageSize,userName,realName));
	}
	
	
	public void add() {
		render("add_edit.html");
	}
	
	public void save() {
		Account account = getBean(Account.class,"account");
		renderJson(accountService.save(account));
	}
	
	public void edit() {
		String id=getPara("id");
		System.out.println(id);
		Account account = accountService.findById(id);
		setAttr("account",account);
		render("add_edit.html");
	}
	
	public void update() {
		Account account = getBean(Account.class,"account");
		renderJson(accountService.update(account));
	}
	
	
	public void delete() {
		String id=get("id");
		renderJson(accountService.deleteAccount(id));
	}
	
	/**
	 * 账户锁定
	 */
	public void lock() {
		Ret ret = accountService.lock(getLoginAccountId(), getPara("id"));
		renderJson(ret);
	}

	/**
	 * 账户解锁
	 */
	public void unlock() {
		Ret ret = accountService.unlock(getPara("id"));
		renderJson(ret);
	}
	
	

	/**
	 * 分配角色
	 */
	public void assignRoles() {
		Account account = accountService.findById(getPara("id"));
		List<Role> roleList = roleService.getAllRoles();
		accountService.markAssignedRoles(account, roleList);
		setAttr("account", account);
		setAttr("roleList", roleList);
		render("assign_roles.html");
	}
	
	/**
	 * 添加角色
	 */
	public void addRole() {
		Ret ret = accountService.addRole(getPara("accountId"), getPara("roleId"));
		renderJson(ret);
	}

	/**
	 * 删除角色
	 */
	public void deleteRole() {
		Ret ret = accountService.deleteRole(getPara("accountId"), getPara("roleId"));
		renderJson(ret);
	}

	/**
	 * 个人中心页面
	 */
	public void personalCenter(){
		Account loginAccount=getLoginAccount();
		setAttr("account",accountService.hidePassword(loginAccount));
		render("personal_center.html");
	}

	/**
	 * 自己修改自己个人信息
	 */
	@Before(PersonalCenterValudator.class)
	public void updatePersonalInformation(){
		Account loginAccount=getLoginAccount();
        String password=getPara("password");
        String rePassword=getPara("repassword");
        renderJson(accountService.updatePersonalCenter(loginAccount,password,rePassword));
	}

}
